﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ITProject.Domain.Entities;
using System.ComponentModel.DataAnnotations;

namespace ITProject.WebUI.Models.ProjectAdmin
{
    public class ProjectAdminEditTeamViewModel
    {
        [Required(ErrorMessage = "Musisz podać nazwę zespołu")]
        [Display(Name = "Nazwa zespołu")]
        [MaxLength(30, ErrorMessage = "Nazwa zespołu może mieć maksymalnie 30 znaków")]
        [MinLength(4, ErrorMessage = "Nazwa zespołu musi zawierać co najmniej 4 znaki")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Musisz podać opis zespołu")]
        [Display(Name = "Opis zespołu")]
        public string Description { get; set; }
        
        public List<SelectListItem> TeamUsers { get; set; }
        
        [Required(ErrorMessage = "Musisz wybrać lidera zespołu")]
        [Display(Name = "Lider zespołu")]
        public string LeaderLogin { get; set; }

        // atrybuty ukryte
        public string ProjectName { get; set; }
        public int TeamId { get; set; }

        public static void CompleteTeamUsers(ProjectAdminEditTeamViewModel vm, string leader, string [] teamUsers)
        {
            vm.TeamUsers = new List<SelectListItem>();
            vm.TeamUsers.Add(new SelectListItem { Text=leader, Selected=true });
            for (int i=0; i<teamUsers.Length; i++)
                vm.TeamUsers.Add(new SelectListItem { Text=teamUsers[i], Selected=false } );
        }

        public static ProjectAdminEditTeamViewModel Convert(Team team, string leader, string [] teamUsers)
        {
            ProjectAdminEditTeamViewModel vm = new ProjectAdminEditTeamViewModel();
            vm.Name = team.Name;
            vm.Description = team.Description;
            vm.TeamId = team.Id;
            CompleteTeamUsers(vm, leader, teamUsers);
            return vm;
        }

        public static Team Convert(ProjectAdminEditTeamViewModel vm, int leaderId)
        {
            Team team = new Team();
            team.Id = vm.TeamId;
            team.Name = vm.Name;
            team.Description = vm.Description;
            team.LeaderId = leaderId;
            return team;
        }
    }
}